<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        class Node {
            constructor(element) {
                this.element = element;
                this.next = null;
                this.prev = null;
            }
        }

        class DoublyLinkedList {
            constructor() {
                this.length = 0;
                this.head = null;
                this.tail = null;
            }

            // 在任意位置添加元素
            insert(position, element) {
                if (position >= 0 && position <= this.length) {

                    let node = new Node(element),
                        current = this.head,
                        index = 0,
                        previous;

                    if (position === 0) {

                        if (!this.head) {

                            this.head = node;
                            this.tail = node;
                        } else {
                            node.next = current;
                            current.prev = node;
                            this.head = node;
                        }
                    } else if (position === this.length) {

                        current = this.tail;
                        current.next = node;
                        node.prev = current;
                        this.tail = node;
                    } else {

                        while (index++ < position) {
                            previous = current;
                            current = current.next;
                        }

                        node.next = current;
                        node.prev = previous;
                        previous.next = node;
                        current.prev = node;
                    }
                    this.length++;
                    return true;
                } else {
                    return false;
                }
            }

            // 在任意位置移除元素
            remove(position) {
                if (position >= 0 && position < this.length) {
                    let index = 0,
                        current = this.head,
                        previous;

                    if (position === 0) {
                        this.head = current.next;
                        if (this.length === 1) {
                            this.tail = null;
                        } else {
                            this.head.prev = null;
                        }
                    } else if (position === this.length - 1) {
                        current = this.tail;
                        this.tail = current.prev;
                        this.tail.next = null;
                    } else {
                        while (index++ < position) {
                            previous = current;
                            current = current.next;
                        }

                        previous.next = current.next;
                        current.next.prev = previous;
                    }

                    this.length--;
                    return current.element;
                } else {
                    return null;
                }
            }
        }

        let list = new DoublyLinkedList();
        list.insert(0, 0);
        list.insert(1, 1);
        list.insert(2, 2);
        console.log(list);
        list.remove(2);
        console.log(list);
    </script>
</body>

</html>